//
//  UIBarbuttonItem+Extension.swift
//  Swift-Categary
//
//  Created by lantaiyuan on 2017/9/11.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import Foundation
import UIKit
private let kRightBtnFont : UIFont = UIFont.fontOfSize(14)//字体大小
extension UIBarButtonItem {
    
    /// 返回一个UIBarButtonItem
    ///
    /// - Parameters:
    ///   - target: 目标
    ///   - action: 监听方法
    ///   - image: 图片名称
    ///   - hightImage: 图片名称
    /// - Returns: UIBarButtonItem
    class func itemWithTargetActionImageHeightImage(target:AnyObject,action:Selector,image:String,hightImage:String) ->UIBarButtonItem{
        // 设置右边的更多按钮
        let btn = UIButton.init(type: .custom)
        btn.addTarget(target, action: action, for: .touchUpInside)
        // 设置图片
        btn.setBackgroundImage(UIImage.init(named: image), for: .normal)
        btn.setBackgroundImage(UIImage.init(named: hightImage), for: .highlighted)
        
        // 设置尺寸
        btn.size = (btn.currentBackgroundImage?.size)!;
        
        btn.x = -20;
        return UIBarButtonItem.init(customView: btn)
    }
    
    class func itemWithTargetActionTitleTitleColor(target:Any,action:Selector,title:String,titleColor:UIColor) -> UIBarButtonItem {
        // 设置右边的更多按钮
        let btn = UIButton.init(type: .custom)
        btn.addTarget(target, action: action, for: .touchUpInside)
        
        // 设置按钮属性
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(titleColor, for: .normal)
        
        btn.titleLabel?.font = kRightBtnFont
        
        // 设置尺寸
        let dict = [NSFontAttributeName:kRightBtnFont]
        let size = (title as String).size(attributes: dict) //Even stranger errors go here
        
        btn.width = size.width;
        
        return UIBarButtonItem.init(customView: btn)
    }
    
    class func item() -> UIBarButtonItem {
        // 设置右边的更多按钮
        let btn = UIButton.init(type: .custom)
        return UIBarButtonItem.init(customView: btn)
    }
    class func itemWithTargetActionNomalImageImageSelectedScale(target:AnyObject,action:Selector,nomalImage:UIImage,imageSelected:UIImage,scale:CGFloat) -> UIBarButtonItem {
        
        let btn = UIButton.init()
        btn.bounds = CGRect(x:0,y:0,width:nomalImage.size.width * scale,height:nomalImage.size.height * scale)
        btn.setImage(nomalImage, for: .normal)
        btn.setImage(imageSelected, for: .highlighted)
        
        //设置按钮监听事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        return UIBarButtonItem.init(customView: btn)
    }
    
    class func itemWithTargetActionTitleFont(target:AnyObject,action:Selector,title:String,font:UIFont) -> UIBarButtonItem {
        
        let btn = UIButton.init(frame: CGRect(x:0,y:0,width:80,height:40))
        btn.titleLabel?.font = font;
        btn.setTitle(title, for: .normal)
        //设置按钮监听事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        return UIBarButtonItem.init(customView: btn)
    }
    class func itemWithTargetActionTitleFontColor(target:AnyObject,action:Selector,title:String,font:UIFont,color:UIColor) -> UIBarButtonItem {
        let btn = UIButton.init(frame: CGRect(x:0,y:0,width:80,height:40))
        
        btn.titleLabel?.font = font;
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(color, for: .normal)
        //设置按钮监听事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        
        return UIBarButtonItem.init(customView: btn)
    }
    
    /*
     创建导航栏左右侧按钮
     */
    static func createBBIWithFrameTitleImageTargetSEL(frame:CGRect,title:String,image:String,target:AnyObject,sel:Selector) -> UIButton {
        let button = UIButton.init(type: .custom)
        button.frame = frame
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setBackgroundImage(UIImage.init(named: image), for: .normal)
        button.addTarget(target, action: sel, for: .touchUpInside)
        return button
    }
    static func createButtonWithFrameTitleImageTargetSel(frame:CGRect,title:String,image:UIImage,target:AnyObject,sel:Selector) -> UIButton {
        let button = UIButton.init(type: .custom)
        button.setImage(image, for: .normal)
        button.frame = frame
        var newFrame = frame
        newFrame.origin.y = frame.height / 2
        newFrame.size.height = frame.height / 2
        newFrame.origin.x = 0
        
        let label = UILabel.init(frame: newFrame)
        label.text = title
        button.addSubview(label)
        
        label.font = UIFont.systemFont(ofSize: 12)
        label.backgroundColor = UIColor.clear
        
        button.addTarget(target, action: sel, for: .touchUpInside)
        return button
    }
    
    // MARK: -- Item 文字
    
    /// Item 文字 左边
    class func itemTitleLeft(_ title:String,titleColor:UIColor,target:AnyObject?,action:Selector) -> UIBarButtonItem {
        return self.itemTitleLeft(title, titleColor: titleColor, titleEdgeInsets : UIEdgeInsetsMake(0, 0, 0, 0),target: target, action: action)
    }
    /*Item 文字 左边*/
    class func itemTitleLeft(_ title:String,titleColor:UIColor,titleEdgeInsets:UIEdgeInsets,target:AnyObject?,action:Selector) -> UIBarButtonItem{
        
        return self.item(title, titleColor: titleColor, contentHorizontalAlignment: UIControlContentHorizontalAlignment.left, titleEdgeInsets: titleEdgeInsets, target: target, action: action)
    }
    
    /// Item 文字 右边
    class func itemTitleRight(_ title:String,titleColor:UIColor,target:AnyObject?,action:Selector) -> UIBarButtonItem {
        return self.itemTitleRight(title, titleColor: titleColor, titleEdgeInsets : UIEdgeInsetsMake(0, 0, 0, 0),target: target, action: action)
    }
    /*Item 文字 右边*/
    class func itemTitleRight(_ title:String,titleColor:UIColor,titleEdgeInsets:UIEdgeInsets,target:AnyObject?,action:Selector) -> UIBarButtonItem{
        
        return self.item(title, titleColor: titleColor, contentHorizontalAlignment: UIControlContentHorizontalAlignment.right, titleEdgeInsets: titleEdgeInsets, target: target, action: action)
    }
    
    
    /// 文字item
    ///
    /// - Parameters:
    ///   - title: 文字
    ///   - titleColor: 文字颜色
    ///   - contentHorizontalAlignment: label位置
    ///   - titleEdgeInsets: label偏移
    ///   - target: 事件对象
    ///   - action: 事件
    class func item(_ title:String,titleColor:UIColor,contentHorizontalAlignment:UIControlContentHorizontalAlignment,titleEdgeInsets:UIEdgeInsets,target:AnyObject?,action:Selector) -> UIBarButtonItem {
        let itemButton  = UIButton(type:UIButtonType.custom)
        itemButton.titleLabel?.font = kRightBtnFont
        itemButton.setTitle(title, for: .normal)
        itemButton.setTitleColor(titleColor, for: .normal)
        itemButton.bounds = CGRect(x:0,y:0,width:40,height:40)
        itemButton.contentHorizontalAlignment = contentHorizontalAlignment
        itemButton.titleEdgeInsets = titleEdgeInsets
        itemButton.addTarget(target, action: action, for: .touchUpInside)
        
        return UIBarButtonItem(customView:itemButton)
    }
    
    // MARK: -- Item Image
    
    /// ImageName Item
    class func itemImageName(_ imageName:String,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        return self.itemImage(UIImage(named: imageName)!, target: target, action: action)
    }
    
    /// ImageName Item
    class func itemImageName(_ imageName:String,imageEdgeInsets:UIEdgeInsets,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        return self.item(UIImage(named: imageName)!, imageEdgeInsets: imageEdgeInsets, target: target, action: action)
    }
    
    /// Image Item
    class func itemImage(_ image:UIImage,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        return self.item(image, imageEdgeInsets: UIEdgeInsetsMake(0, 0, 0, 0), target: target, action: action)
    }
    
    /**
     图片Item
     
     - parameter image:  图片
     - parameter target: 事件对象
     - parameter imageEdgeInsets:            Image偏移
     - parameter action: 事件
     */
    class func item(_ image:UIImage,imageEdgeInsets:UIEdgeInsets,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        let itemButton = UIButton(type:UIButtonType.custom)
        itemButton.setImage(image, for: UIControlState())
        itemButton.bounds = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
        itemButton.imageEdgeInsets = imageEdgeInsets
        itemButton.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        return UIBarButtonItem(customView: itemButton)
    }
    
    
    // MARK: -- 自定义 Item
    
    /// 自定义点击View
    class func itemControl(_ control:UIControl,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        control.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        return UIBarButtonItem(customView: control)
    }
    
    class func itemButton(_ button:UIButton,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        button.titleLabel?.font = kRightBtnFont
        
        button.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        return UIBarButtonItem(customView: button)
    }
    
    
    // MARK: -- 返回按钮
    
    /// app 导航栏返回按钮 类型 1
    class func AppNavigationBarBackItemOne(_ imageEdgeInsets:UIEdgeInsets,target:AnyObject?,action:Selector) ->UIBarButtonItem {
        
        return self.itemImageName("Back_0", imageEdgeInsets: imageEdgeInsets, target: target, action: action)
    }
}
